import { JSFuncs } from 'mixly';
import * as Blockly from 'blockly/core';


export const mixly_lib_llm_init = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_KEY = generator.valueToCode(this, 'KEY', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_MODEL = generator.valueToCode(this, 'MODEL', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_PROMPT = generator.valueToCode(this, 'PROMPT', generator.ORDER_ATOMIC) || '';
    generator.definitions_['include_AIChatbot'] = '#include <AIChatbot.h>';
    if (JSFuncs.getPlatform() === 'Arduino ESP8266') {
        generator.definitions_['include_ESP8266WiFi'] = '#include <ESP8266WiFi.h>';
    } else {
        generator.definitions_['include_WiFi'] = '#include <WiFi.h>';
    }
    generator.definitions_[`var_declare_${FIELD_NAME}`] = `AIChatbot ${FIELD_NAME}(${VALUE_INPUT_KEY}, ${VALUE_INPUT_MODEL}, ${VALUE_INPUT_PROMPT});`;
    return '';
};

export const mixly_lib_llm_models = function (_, generator) {
    const FIELD_PROVIDER = this.getFieldValue('PROVIDER') || '';
    if (FIELD_PROVIDER === 'Custom') {
        return ['""', generator.ORDER_ATOMIC];
    }
    const FIELD_MODEL = this.getFieldValue('MODEL') || '';
    return [`"${FIELD_PROVIDER}@${FIELD_MODEL}"`, generator.ORDER_ATOMIC];
};

export const mixly_lib_llm_prompts_template = function (_, generator) {
    const FIELD_PROMPT = this.getFieldValue('PROMPT') || '';
    const item = Blockly.Msg.MIXLY_LIB_ARDUINO_LLM_PROMPTS_TEMPLATE_MAP[FIELD_PROMPT];
    generator.definitions_[`define_SYSTEM_PROMPT_${item['name']}`] = `#define SYSTEM_PROMPT_${item['name']} "${item['prompt']}"`;
    return [`SYSTEM_PROMPT_${item['name']}`, generator.ORDER_ATOMIC];
};

export const mixly_lib_llm_set_api_key = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_KEY = generator.valueToCode(this, 'KEY', generator.ORDER_ATOMIC) || '';
    return `${FIELD_NAME}.setKey(${VALUE_INPUT_KEY});\n`;
};

export const mixly_lib_llm_set_timeout = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'NUM', generator.ORDER_ATOMIC) || 10000;
    generator.definitions_['include_AIChatbot'] = '#include <AIChatbot.h>';
    return `${FIELD_NAME}.setTimeout(${VALUE_INPUT_NUM});\n`;
};

export const mixly_lib_llm_set_max_retries = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'NUM', generator.ORDER_ATOMIC) || -1;
    generator.definitions_['include_AIChatbot'] = '#include <AIChatbot.h>';
    return `${FIELD_NAME}.setMaxRetries(${VALUE_INPUT_NUM});\n`;
};

export const mixly_lib_llm_set_system_prompt = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_PROMPT = generator.valueToCode(this, 'PROMPT', generator.ORDER_ATOMIC) || '';
    return `${FIELD_NAME}.setSystemPrompt(${VALUE_INPUT_PROMPT});\n`;
};

export const mixly_lib_llm_set_model = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_MODEL = generator.valueToCode(this, 'MODEL', generator.ORDER_ATOMIC) || '';
    return `${FIELD_NAME}.selectAI(${VALUE_INPUT_MODEL});\n`;
};

export const mixly_lib_llm_model_provider = function (_, generator) {
    const FIELD_PROVIDER = this.getFieldValue('PROVIDER') || 'Custom';
    return [`"${FIELD_PROVIDER}"`, generator.ORDER_ATOMIC];
};

export const mixly_lib_llm_set_custom_model = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_PROVIDER = generator.valueToCode(this, 'PROVIDER', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_MODEL = generator.valueToCode(this, 'MODEL', generator.ORDER_ATOMIC) || '';
    return `${FIELD_NAME}.selectAI(${VALUE_INPUT_PROVIDER}, ${VALUE_INPUT_MODEL});\n`;
};

export const mixly_lib_llm_set_custom_url = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'STR', generator.ORDER_ATOMIC) || -1;
    return `${FIELD_NAME}.setCustomUrl(${VALUE_INPUT_NUM});\n`;
};

export const mixly_lib_llm_enable_history_messages = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_BOOL = generator.valueToCode(this, 'BOOL', generator.ORDER_ATOMIC) || 'false';
    return `${FIELD_NAME}.enableHistoryMessages(${VALUE_INPUT_BOOL});\n`;
};

export const mixly_lib_llm_set_history_messages_num = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'NUM', generator.ORDER_ATOMIC) || -1;
    return `${FIELD_NAME}.setHistoryMessagesNum(${VALUE_INPUT_NUM});\n`;
};

export const mixly_lib_llm_chat = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_PROMPT = generator.valueToCode(this, 'PROMPT', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'NUM', generator.ORDER_ATOMIC) || 1024;
    return [`${FIELD_NAME}.chat(${VALUE_INPUT_PROMPT}, ${VALUE_INPUT_NUM})`, generator.ORDER_ATOMIC];
};

export const mixly_lib_llm_chat_stream = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_PROMPT = generator.valueToCode(this, 'PROMPT', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'NUM', generator.ORDER_ATOMIC) || 1024;
    const VALUE_INPUT_CALLBACK = generator.valueToCode(this, 'CALLBACK', generator.ORDER_ATOMIC) || '';
    return `${FIELD_NAME}.chat(${VALUE_INPUT_PROMPT}, ${VALUE_INPUT_NUM}, ${VALUE_INPUT_CALLBACK});\n`;
};

export const mixly_lib_llm_chat_stream2 = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_PROMPT = generator.valueToCode(this, 'PROMPT', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'NUM', generator.ORDER_ATOMIC) || 1024;
    const VALUE_INPUT_CALLBACK = generator.valueToCode(this, 'CALLBACK', generator.ORDER_ATOMIC) || '';
    return [`${FIELD_NAME}.chat(${VALUE_INPUT_PROMPT}, ${VALUE_INPUT_NUM}, ${VALUE_INPUT_CALLBACK})`, generator.ORDER_ATOMIC];
};

export const mixly_lib_llm_chat_stream_reasoning = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_PROMPT = generator.valueToCode(this, 'PROMPT', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'NUM', generator.ORDER_ATOMIC) || 1024;
    const VALUE_INPUT_CALLBACK1 = generator.valueToCode(this, 'CALLBACK1', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_CALLBACK2 = generator.valueToCode(this, 'CALLBACK2', generator.ORDER_ATOMIC) || '';
    return `${FIELD_NAME}.chat(${VALUE_INPUT_PROMPT}, ${VALUE_INPUT_NUM}, ${VALUE_INPUT_CALLBACK1}, ${VALUE_INPUT_CALLBACK2});\n`;
};

export const mixly_lib_llm_chat_stream_reasoning2 = function (_, generator) {
    const FIELD_NAME = this.getFieldValue('NAME') || 'chatbot';
    const VALUE_INPUT_PROMPT = generator.valueToCode(this, 'PROMPT', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_NUM = generator.valueToCode(this, 'NUM', generator.ORDER_ATOMIC) || 1024;
    const VALUE_INPUT_CALLBACK1 = generator.valueToCode(this, 'CALLBACK1', generator.ORDER_ATOMIC) || '';
    const VALUE_INPUT_CALLBACK2 = generator.valueToCode(this, 'CALLBACK2', generator.ORDER_ATOMIC) || '';
    return [`${FIELD_NAME}.chat(${VALUE_INPUT_PROMPT}, ${VALUE_INPUT_NUM}, ${VALUE_INPUT_CALLBACK1}, ${VALUE_INPUT_CALLBACK2})`, generator.ORDER_ATOMIC];
};